using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace RPGOnline.Controls.Map.Layers.FogOfWar
{
    public class FogOfWarLayer
    {
        private readonly Device m_device;
        private readonly Map m_map;
        private FogOfWarType[,] m_fogOfWar;
        private VertexBuffer[] m_quads;

        public FogOfWarLayer(Device device, Map map)
        {
            m_device = device;
            m_map = map;
            m_fogOfWar = new FogOfWarType[m_map.MapSize.Width, m_map.MapSize.Height];
            m_quads = new VertexBuffer[3];
            m_quads[(int) FogOfWarType.Invisible] = new VertexBuffer(typeof(CustomVertex.PositionColored), 4, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);
            m_quads[(int)FogOfWarType.Invisible].SetData(new CustomVertex.PositionColored[4]
            {
                new CustomVertex.PositionColored(0, 0, 0, Color.Black.ToArgb()),
                new CustomVertex.PositionColored(m_map.TileSize.Width, 0, 0, Color.Black.ToArgb()),
                new CustomVertex.PositionColored(0, m_map.TileSize.Height, 0, Color.Black.ToArgb()),
                new CustomVertex.PositionColored(m_map.TileSize.Width, m_map.TileSize.Height, 0, Color.Black.ToArgb())
            }, 0, LockFlags.None);

            m_quads[(int) FogOfWarType.Fogged] = new VertexBuffer(typeof(CustomVertex.PositionColored), 4, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);
            m_quads[(int)FogOfWarType.Fogged].SetData(new CustomVertex.PositionColored[4]
            {
                new CustomVertex.PositionColored(0, 0, 0, Color.FromArgb(128, Color.Black).ToArgb()),
                new CustomVertex.PositionColored(m_map.TileSize.Width, 0, 0, Color.FromArgb(128, Color.Black).ToArgb()),
                new CustomVertex.PositionColored(0, m_map.TileSize.Height, 0, Color.FromArgb(128, Color.Black).ToArgb()),
                new CustomVertex.PositionColored(m_map.TileSize.Width, m_map.TileSize.Height, 0, Color.FromArgb(128, Color.Black).ToArgb())
            }, 0, LockFlags.None);
        }

        public void Draw()
        {
            m_device.RenderState.AlphaBlendEnable = true;

            DrawTilesByType(FogOfWarType.Invisible);
            DrawTilesByType(FogOfWarType.Fogged);

            m_device.Transform.World = Matrix.Identity;
            m_device.RenderState.AlphaBlendEnable = false;
        }

        private void DrawTilesByType(FogOfWarType fogOfWarType)
        {
            m_device.VertexFormat = m_quads[(int) fogOfWarType].Description.VertexFormat;
            m_device.SetStreamSource(0, m_quads[(int) fogOfWarType], 0);

            for(int y = 0; y < m_map.MapSize.Height; y++)
            {
                for(int x = 0; x < m_map.MapSize.Width; x++)
                {
                    if (fogOfWarType != m_fogOfWar[x, y])
                        continue;

                    DrawTile(x, y);
                }
            }
        }

        private void DrawTile(int x, int y)
        {
            m_device.Transform.World = Matrix.Translation(x * m_map.TileSize.Width, (m_map.MapSize.Height * m_map.TileSize.Height) - (y * m_map.TileSize.Height), 0);
            m_device.Transform.World.Scale(0, -1, 0);

            m_device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
        }

        public void Update(int x, int y, int radiusInCells)
        {
        }
    }
}